library(tidyverse)

DATA <- tribble(
  ~FACET        , ~GROUP                    , ~PE      , ~CILO     , ~CIHI    , 
  "Full sample" , "Rated immigrants 0 to 25", 0.1723133, 0.135205  , 0.2094216, 
  "Full sample" , "Rated immigrants at 50"  , 0.3424786, 0.2964904 , 0.3884669, 
  "Full sample" , "Rated immigrants at 100" , 0.7593085, 0.7157112 , 0.8029057, 
  "Men"         , "Rated immigrants 0 to 25", 0.1560584, 0.1054527 , 0.2066641, 
  "Men"         , "Rated immigrants at 50"  , 0.2770078, 0.2172838 , 0.3367318, 
  "Men"         , "Rated immigrants at 100" , 0.6433072, 0.5703725 , 0.7162419, 
  "Women"       , "Rated immigrants 0 to 25", 0.200371 , 0.1509051 , 0.2498369, 
  "Women"       , "Rated immigrants at 50"  , 0.4399089, 0.3678592 , 0.5119586, 
  "Women"       , "Rated immigrants at 100" , 0.882532 , 0.847249  , 0.9178149, 
  "Age 18-30"   , "Rated immigrants 0 to 25", 0.1601688, 0.0401694 , 0.2801683, 
  "Age 18-30"   , "Rated immigrants at 50"  , 0.3337109, 0.2067288 , 0.460693 , 
  "Age 18-30"   , "Rated immigrants at 100" , 0.9110231, 0.8459836 , 0.9760625, 
  "Age 31-45"   , "Rated immigrants 0 to 25", 0.131409 , 0.0715108 , 0.1913073, 
  "Age 31-45"   , "Rated immigrants at 50"  , 0.3588245, 0.2595524 , 0.4580965, 
  "Age 31-45"   , "Rated immigrants at 100" , 0.7044307, 0.5912598 , 0.8176015, 
  "Age 46-60"   , "Rated immigrants 0 to 25", 0.2212837, 0.1644624 , 0.2781049, 
  "Age 46-60"   , "Rated immigrants at 50"  , 0.3656777, 0.2889871 , 0.4423682, 
  "Age 46-60"   , "Rated immigrants at 100" , 0.7223355, 0.6540197 , 0.7906514, 
  "Age 61+"     , "Rated immigrants 0 to 25", 0.1415681, 0.0935117 , 0.1896246, 
  "Age 61+"     , "Rated immigrants at 50"  , 0.3781562, 0.2974072 , 0.4589051, 
  "Age 61+"     , "Rated immigrants at 100" , 0.7878973, 0.727787  , 0.8480077, 
  "Whites"      , "Rated immigrants 0 to 25", 0.089659 , 0.0643399 , 0.1149781, 
  "Whites"      , "Rated immigrants at 50"  , 0.2592159, 0.2183835 , 0.3000483, 
  "Whites"      , "Rated immigrants at 100" , 0.6962087, 0.6486655 , 0.743752 , 
  "Non-Whites"  , "Rated immigrants 0 to 25", 0.3962837, 0.2988759 , 0.4936914, 
  "Non-Whites"  , "Rated immigrants at 50"  , 0.4992125, 0.3982557 , 0.6001692, 
  "Non-Whites"  , "Rated immigrants at 100" , 0.7850707, 0.7052668 , 0.8648746, 
  "Democrats"   , "Rated immigrants 0 to 25", 0.6429413, 0.5125745 , 0.7733081, 
  "Democrats"   , "Rated immigrants at 50"  , 0.9631039, 0.9370234 , 0.9891843, 
  "Democrats"   , "Rated immigrants at 100" , 0.9864279, 0.9734549 , 0.999401 , 
  "Independents", "Rated immigrants 0 to 25", 0.0684644, 0.02679   , 0.1101389, 
  "Independents", "Rated immigrants at 50"  , 0.3371802, 0.2632759 , 0.4110846, 
  "Independents", "Rated immigrants at 100" , 0.6556167, 0.5757245 , 0.735509 , 
  "Republicans" , "Rated immigrants 0 to 25", 0.028494 , 0.0113812 , 0.0456067, 
  "Republicans" , "Rated immigrants at 50"  , 0.0199092, 0.005434  , 0.0343844, 
  "Republicans" , "Rated immigrants at 100" , 0.0802531, -0.0034934, 0.1639995)

DATA$FACET <- factor(DATA$FACET, levels = unique(DATA$FACET))
DATA$GROUP <- factor(DATA$GROUP, levels = rev(unique(DATA$GROUP)))
COLOR.COLD <- "indianred2"
COLOR.WARM <- "darkseagreen3"

theme.z <-   theme(
  axis.text.x        = element_blank(),
  axis.text.x.top    = element_text(size = 15, color = "black", hjust = 1  , margin = margin(t = 8,b = 0)),
  axis.text.y        = element_text(size = 15, color = "black", hjust = 1  , margin = margin(l = 8,r = 8)),
  axis.text.y.right  = element_text(size = 15, color = "black", hjust = 0  , margin = margin(l = 8,r = 8)),
  axis.ticks.x       = element_blank(),
  axis.ticks.y       = element_blank(),
  axis.title.x       = element_text(size = 15, color = "black", hjust = 0.5, margin = margin(t = 8,b = 0)),
  axis.title.y       = element_blank(),
  legend.position    = "none",
  panel.background   = element_rect(linewidth = 1.0, color = "black", fill = "gray90", linetype = "solid"),
  panel.border       = element_rect(linewidth = 1.0, color = "black", fill = NA      , linetype = "solid"),
  panel.grid.major.x = element_blank(), 
  panel.grid.major.y = element_blank(),
  panel.grid.minor.x = element_blank(), 
  panel.grid.minor.y = element_blank(),
  panel.spacing.x    = unit(1, "lines"),
  panel.spacing.y    = unit(1, "lines"),
  plot.background    = element_rect(fill = "white"),
  plot.caption       = element_text(size = 12 , hjust = 0  , margin = margin(t = 10)),
  plot.margin        = unit(c(t = 10,r = 45,b = 10,l = 0),"pt"),
  plot.subtitle      = element_text(size = 15 , hjust = 0.5, margin = margin(b = 10)),
  plot.title         = element_text(size = 17 , hjust = 0.5, margin = margin(t = 0,b = 10), face = "bold"),
  strip.background   = element_rect(linewidth = 1.0, color = "black", fill = "black"),
  strip.placement    = "outside",
  strip.text.x       = element_text(size = 18, color = "white", face = "bold", margin = margin(t = 8, b = 8))
)

ggplot(DATA, aes(PE, GROUP)) +
  facet_wrap(~FACET, nrow = 4, labeller = label_wrap_gen(width=25)) +
  geom_rect(data = filter(DATA, FACET == "Full sample") , aes(xmin = min(DATA$PE[DATA$FACET == "Full sample"]) , xmax = DATA$PE[DATA$FACET == "Full sample"  & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Men")         , aes(xmin = min(DATA$PE[DATA$FACET == "Men"])         , xmax = DATA$PE[DATA$FACET == "Men"          & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Women")       , aes(xmin = min(DATA$PE[DATA$FACET == "Women"])       , xmax = DATA$PE[DATA$FACET == "Women"        & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 18-30")   , aes(xmin = min(DATA$PE[DATA$FACET == "Age 18-30"])   , xmax = DATA$PE[DATA$FACET == "Age 18-30"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 31-45")   , aes(xmin = min(DATA$PE[DATA$FACET == "Age 31-45"])   , xmax = DATA$PE[DATA$FACET == "Age 31-45"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 46-60")   , aes(xmin = min(DATA$PE[DATA$FACET == "Age 46-60"])   , xmax = DATA$PE[DATA$FACET == "Age 46-60"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 61+")     , aes(xmin = min(DATA$PE[DATA$FACET == "Age 61+"])     , xmax = DATA$PE[DATA$FACET == "Age 61+"      & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Whites")      , aes(xmin = min(DATA$PE[DATA$FACET == "Whites"])      , xmax = DATA$PE[DATA$FACET == "Whites"       & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Non-Whites")  , aes(xmin = min(DATA$PE[DATA$FACET == "Non-Whites"])  , xmax = DATA$PE[DATA$FACET == "Non-Whites"   & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Democrats")   , aes(xmin = min(DATA$PE[DATA$FACET == "Democrats"])   , xmax = DATA$PE[DATA$FACET == "Democrats"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Independents"), aes(xmin = min(DATA$PE[DATA$FACET == "Independents"]), xmax = DATA$PE[DATA$FACET == "Independents" & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Republicans") , aes(xmin = min(DATA$PE[DATA$FACET == "Republicans"]) , xmax = DATA$PE[DATA$FACET == "Republicans"  & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.COLD, inherit.aes = FALSE) +
  
  geom_rect(data = filter(DATA, FACET == "Full sample") , aes(xmin = max(DATA$PE[DATA$FACET == "Full sample"]) , xmax = DATA$PE[DATA$FACET == "Full sample"  & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Men")         , aes(xmin = max(DATA$PE[DATA$FACET == "Men"])         , xmax = DATA$PE[DATA$FACET == "Men"          & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Women")       , aes(xmin = max(DATA$PE[DATA$FACET == "Women"])       , xmax = DATA$PE[DATA$FACET == "Women"        & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 18-30")   , aes(xmin = max(DATA$PE[DATA$FACET == "Age 18-30"])   , xmax = DATA$PE[DATA$FACET == "Age 18-30"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 31-45")   , aes(xmin = max(DATA$PE[DATA$FACET == "Age 31-45"])   , xmax = DATA$PE[DATA$FACET == "Age 31-45"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 46-60")   , aes(xmin = max(DATA$PE[DATA$FACET == "Age 46-60"])   , xmax = DATA$PE[DATA$FACET == "Age 46-60"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Age 61+")     , aes(xmin = max(DATA$PE[DATA$FACET == "Age 61+"])     , xmax = DATA$PE[DATA$FACET == "Age 61+"      & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Whites")      , aes(xmin = max(DATA$PE[DATA$FACET == "Whites"])      , xmax = DATA$PE[DATA$FACET == "Whites"       & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Non-Whites")  , aes(xmin = max(DATA$PE[DATA$FACET == "Non-Whites"])  , xmax = DATA$PE[DATA$FACET == "Non-Whites"   & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Democrats")   , aes(xmin = max(DATA$PE[DATA$FACET == "Democrats"])   , xmax = DATA$PE[DATA$FACET == "Democrats"    & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Independents"), aes(xmin = max(DATA$PE[DATA$FACET == "Independents"]), xmax = DATA$PE[DATA$FACET == "Independents" & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +
  geom_rect(data = filter(DATA, FACET == "Republicans") , aes(xmin = max(DATA$PE[DATA$FACET == "Republicans"]) , xmax = DATA$PE[DATA$FACET == "Republicans"  & DATA$GROUP=="Rated immigrants at 50"], ymin = -Inf, ymax = Inf), color = "black", fill = COLOR.WARM, inherit.aes = FALSE) +

  geom_errorbarh(aes(xmin = CILO, xmax = CIHI), height = 0, size = 0.5) +
  geom_point(size = 4) +
  coord_cartesian(clip = "off") +
  scale_x_continuous(limits = c(0,1), expand = c(0,0)) +
  #scale_y_discrete(labels = label_wrap(35)) +
  labs(x = "Predicted probability of a vote for Joe Biden over Donald Trump") +
  theme.z

ggsave(file = "Figure 2.svg", width = 9.5, height = 8)
ggsave(file = "Figure 2.eps", width = 9.5, height = 8)
